{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "47fef522",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 1]\n",
      "insert之后的names： ['Mike', 'yougege', 'Mary', 'Jan', 'Jack', 1]\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "# 1.增加的方法\n",
    "## 1.1 增加  append() 方法用于在列表末尾添加新的对象。\n",
    "names.append(1)\n",
    "print(names)\n",
    "\n",
    "## 1.2 插入 insert() 可以指定位置增加元素\n",
    "names.insert(1,'yougege')  #在0.1.2.3的顺序，1的位置添加\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a09c4cbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： [123, 15, 'abc']\n"
     ]
    }
   ],
   "source": [
    "# 2. 删除 remove  和 pop\n",
    "# 优势：如果删除list中多个元素时，remove比较合适\n",
    "# 2.1 remove()一般情况下 使用remove先用in做判断\n",
    "names = [123,15, 'xyz', 'abc']\n",
    "if 'xyz' in names:    #别忘了对象的‘’引号！！！\n",
    "    names.remove('xyz')\n",
    "print(\"remove删除的结果：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9cfe9114",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的names: ['Mike', 'Mary', 'Jan']\n"
     ]
    }
   ],
   "source": [
    "## 2.2 #pop() 函数用于移除列表中的一个元素（默认最后一个元素），并且返回该元素的值\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8d2fd383",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "## 3. index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "print(stu_id.index(10001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a6ae516a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "## 4. clear()清除所有数据\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7e88fd6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果: 3\n"
     ]
    }
   ],
   "source": [
    "## 5.count()查看列表元素出现的个数，（简单应用）统计分数出现的次数\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "print(\"count方法的结果:\",stu_id.count(10001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0a9bb05f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "## 6. extend() 是给指定的list进行扩充\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "29091061",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 4\n"
     ]
    }
   ],
   "source": [
    "# 列表的其他方法(Sequence Types 的一些方法)\n",
    "# 1. len()查看list长度\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "print(\"names的长度/names的列表元素个数：\",len(names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "15d0eeb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# 2. in 查看元素是否存在list当中,结果为布尔值，False/True,\n",
    "# 一般在操作list的修改和删除时会先判断\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "print(\"suyuqin\" in names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "590e9cbc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
